package com.kyomonn.talkischeap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class WelcomeActivity extends AppCompatActivity {

    private static final int TOTAL_TIME = 3; // 总倒计时时间，单位：秒
    private final Handler handler = new Handler();
    private int remainingTime = TOTAL_TIME;
    private Button btnSkip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        btnSkip = findViewById(R.id.btn_skip);

        // 设置点击跳过
        btnSkip.setOnClickListener(v -> skipToMainActivity());

        // 每隔1秒更新倒计时
        handler.postDelayed(countdownRunnable, 1000);
    }

    private void skipToMainActivity() {
        handler.removeCallbacks(countdownRunnable); // 停止倒计时
        startActivity(new Intent(this, MainActivity.class));
        finish(); // 关闭欢迎页
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(countdownRunnable); // 防止内存泄漏
    }

    private final Runnable countdownRunnable = new Runnable() {
        @Override
        public void run() {
            remainingTime--;
            if (remainingTime > 0) {
                btnSkip.setText(String.format(Locale.CHINA, "跳过 %d", remainingTime));
                handler.postDelayed(this, 1000); // 继续倒计时
            } else {
                skipToMainActivity(); // 时间到，跳转
            }
        }
    };


}
